package org.hxyjs.service.impl;


import org.hxyjs.dao.SalesagentDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Account;
import org.hxyjs.entity.Parents;
import org.hxyjs.entity.Salesagent;
import org.hxyjs.entity.Salesagent;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AccountService;
import org.hxyjs.service.SalesagentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("salesagentService")
public class SalesagentServiceImpl implements SalesagentService, AccountService {
    @Resource
    SalesagentDao salesagentDao;
    @Override
    public DTO<Salesagent> getAllSalesagents()  throws Exception{
        List<Salesagent> salesagents = salesagentDao.getAllSalesagents();
        MessageEnum messageEnum  = Optional.ofNullable(salesagents).isPresent()?MessageEnum.GETSALESAGENTSUCCESS: MessageEnum.GETSALESAGENTFAIL;
        return new DTO<Salesagent>(messageEnum.getCode(),messageEnum.getMessage(),salesagents);
    }

    @Override
    public DTO<Salesagent> deleteSalesagentBySano(String sano)  throws Exception{
        Integer result = salesagentDao.deleteSalesagentBySano(sano);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETESALESAGENTSUCCESS:MessageEnum.DELETESALESAGENTFAIL;
        return new DTO<Salesagent>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Salesagent> updateSalesagentBySano(Salesagent salesagent)  throws Exception{
        Integer result = salesagentDao.updateSalesagentBySano(salesagent);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATESALESAGENTSUCCESS:MessageEnum.UPDATESALESAGENTFAIL;
        return new DTO<Salesagent>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Salesagent> addSalesagent(Salesagent salesagent) throws Exception {
        Integer result = salesagentDao.addSalesagent(salesagent);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDSALESAGENTSUCCESS:MessageEnum.ADDSALESAGENTFAIL;
        return new DTO<Salesagent>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Salesagent> getSalesagentByPage(PageInfo pageInfo ) throws Exception {
        List<Salesagent> salesagents = salesagentDao.getSalesagentByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(salesagents).isPresent()?MessageEnum.GETSALESAGENTSUCCESS: MessageEnum.GETSALESAGENTFAIL;
        return new DTO<Salesagent>(messageEnum.getCode(),messageEnum.getMessage(),salesagents,pageInfo);
    }

    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        Salesagent salesagents = salesagentDao.getAccountByAccountName(accountName);
        MessageEnum messageEnum  = Optional.ofNullable(accountName).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Account>(messageEnum.getCode(),messageEnum.getMessage(),salesagents);
    }
}
